package com.example.hospital.controller;

import com.example.hospital.dto.PharmacyinventoryQuery;
import com.example.hospital.dto.Result;
import com.example.hospital.entity.Pharmacyinventory;
import com.example.hospital.service.PharmacyinventoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("pharmacyinventory")
@Tag(name = "PharmacyInventory", description = "药品库存管理相关接口")
public class PharmacyinventoryController {
    @Resource
    private PharmacyinventoryService pharmacyinventoryService;

    /**
     * 新增药品库存
     *
     * @param pharmacyinventory
     * @return pharmacyinventory id or error
     */
    @Operation(
            summary = "新增药品库存",
            description = "创建一个新的药品库存记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "添加成功, 返回库存ID"),
                    @ApiResponse(responseCode = "400", description = "请求参数错误")
            }
    )
    @PostMapping
    public Result<Integer> addPharmacyinventory(@RequestBody Pharmacyinventory pharmacyinventory) {
        boolean saved = pharmacyinventoryService.save(pharmacyinventory);
        return saved ? Result.success("添加成功", pharmacyinventory.getInventoryId()) : Result.fail("添加失败");
    }

    /**
     * 根据id删除药品库存
     *
     * @param id
     * @return
     */
    @Operation(
            summary = "根据id删除药品库存",
            description = "通过药品库存ID删除指定的库存记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "删除成功"),
                    @ApiResponse(responseCode = "400", description = "库存记录未找到")
            }
    )
    @DeleteMapping("/{id}")
    public Result deletePharmacyinventory(@PathVariable Long id) {
        boolean removed = pharmacyinventoryService.removeById(id);
        return removed ? Result.success("删除成功") : Result.fail("删除失败");
    }

    /**
     * 更新药品库存
     *
     * @param pharmacyinventory
     * @return
     */
    @Operation(
            summary = "更新药品库存",
            description = "通过传递新的库存信息更新现有库存",
            responses = {
                    @ApiResponse(responseCode = "200", description = "更新成功"),
                    @ApiResponse(responseCode = "400", description = "请求参数错误")
            }
    )
    @PutMapping
    public Result updatePharmacyinventory(@RequestBody Pharmacyinventory pharmacyinventory) {
        boolean updated = pharmacyinventoryService.updateById(pharmacyinventory);
        return updated ? Result.success("更新成功") : Result.fail("更新失败");
    }

    /**
     * 根据id查询药品库存
     *
     * @param id
     * @return pharmacyinventory or error
     */
    @Operation(
            summary = "根据id查询药品库存",
            description = "根据库存ID查询药品库存信息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "400", description = "库存记录未找到")
            }
    )
    @GetMapping("/{id}")
    public Result<Pharmacyinventory> getPharmacyinventory(@PathVariable Long id) {
        Pharmacyinventory pharmacyinventory = pharmacyinventoryService.getById(id);
        return pharmacyinventory != null ? Result.success("查询成功", pharmacyinventory) : Result.fail("查询失败");
    }

    /**
     * 查询药品库存列表
     *
     * @return pharmacyinventory list
     */
    @Operation(
            summary = "查询药品库存列表",
            description = "获取所有药品库存记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "500", description = "服务器错误")
            }
    )
    @GetMapping
    public Result<List<Pharmacyinventory>> listPharmacyinventorys() {
        return Result.success(pharmacyinventoryService.list());
    }

    /**
     * 条件查询药品库存
     *
     * @param pharmacyinventoryQuery
     * @return pharmacyinventory list
     */
    @Operation(
            summary = "条件查询药品库存",
            description = "根据条件查询药品库存记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "500", description = "服务器错误")
            }
    )
    @PostMapping("/query")
    public Result<List<Pharmacyinventory>> queryPharmacyinventory(@RequestBody PharmacyinventoryQuery pharmacyinventoryQuery) {
        return Result.success(pharmacyinventoryService.listPharmacyinventorys(pharmacyinventoryQuery));
    }
}
